package com.sangsang.kafka.domain.dto;

import com.alibaba.fastjson.annotation.JSONField;
import com.sangsang.kafka.domain.constants.SymbolConstant;

import java.util.Objects;

/**
 * @author liutangqi
 * @date 2023/12/21 18:23
 */
public class SourceDto implements Comparable<SourceDto> {
    /**
     * debezium的版本号
     */
    private String version;
    /**
     * 连接器类型，比如 mysql
     */
    private String connector;

    /**
     * 连接器的 database.server.name
     */
    private String name;
    /**
     * 时间
     */
    @JSONField(name = "ts_ms")
    private Long ts_ms;
    /**
     * 快照
     */
    private String snapshot;
    /**
     * database的名字
     */
    private String db;
    /**
     * 顺序
     */
    private Object sequence;
    /**
     * 表名
     */
    private String table;
    /**
     * 伪装的从节点的 服务id
     */
    @JSONField(name = "server_id")
    private Long server_id;
    /**
     * 全局事务id
     */
    private String gtid;

    /**
     * binlog的文件名
     */
    private String file;
    /**
     * 当前binlog的位置
     */
    private Long pos;
    /**
     *
     */
    private Long row;
    private Object thread;
    private Object query;

    /**
     * 覆写比较的方法
     * 注意：这里将默认的升序排序修改成了降序，为了后续代码结构好看一点
     *
     * @author liutangqi
     * @date 2023/12/26 14:51
     * @Param [obj]
     **/
    @Override
    public int compareTo(SourceDto obj) {
        //优先根据binlog文件名排序
        String thisFileNum = this.getFile().substring(this.getFile().lastIndexOf(SymbolConstant.FULL_STOP) + 1);
        String objFileNum = obj.getFile().substring(obj.getFile().lastIndexOf(SymbolConstant.FULL_STOP) + 1);

        int res = Long.valueOf(objFileNum).compareTo(Long.valueOf(thisFileNum));
        if (res == 0) {
            //binlog文件名一样，根据pos点排序
            res = obj.getPos().compareTo(this.getPos());
        }
        return res;
    }


    public static SourceDto.SourceDtoBuilder builder() {
        return new SourceDto.SourceDtoBuilder();
    }

    public String getVersion() {
        return this.version;
    }

    public String getConnector() {
        return this.connector;
    }

    public String getName() {
        return this.name;
    }

    public Long getTs_ms() {
        return this.ts_ms;
    }

    public String getSnapshot() {
        return this.snapshot;
    }

    public String getDb() {
        return this.db;
    }

    public Object getSequence() {
        return this.sequence;
    }

    public String getTable() {
        return this.table;
    }

    public Long getServer_id() {
        return this.server_id;
    }

    public String getGtid() {
        return this.gtid;
    }

    public String getFile() {
        return this.file;
    }

    public Long getPos() {
        return this.pos;
    }

    public Long getRow() {
        return this.row;
    }

    public Object getThread() {
        return this.thread;
    }

    public Object getQuery() {
        return this.query;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public void setConnector(String connector) {
        this.connector = connector;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setTs_ms(Long ts_ms) {
        this.ts_ms = ts_ms;
    }

    public void setSnapshot(String snapshot) {
        this.snapshot = snapshot;
    }

    public void setDb(String db) {
        this.db = db;
    }

    public void setSequence(Object sequence) {
        this.sequence = sequence;
    }

    public void setTable(String table) {
        this.table = table;
    }

    public void setServer_id(Long server_id) {
        this.server_id = server_id;
    }

    public void setGtid(String gtid) {
        this.gtid = gtid;
    }

    public void setFile(String file) {
        this.file = file;
    }

    public void setPos(Long pos) {
        this.pos = pos;
    }

    public void setRow(Long row) {
        this.row = row;
    }

    public void setThread(Object thread) {
        this.thread = thread;
    }

    public void setQuery(Object query) {
        this.query = query;
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof SourceDto)) {
            return false;
        } else {
            SourceDto other = (SourceDto) o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                label191:
                {
                    Object this$ts_ms = this.getTs_ms();
                    Object other$ts_ms = other.getTs_ms();
                    if (this$ts_ms == null) {
                        if (other$ts_ms == null) {
                            break label191;
                        }
                    } else if (this$ts_ms.equals(other$ts_ms)) {
                        break label191;
                    }

                    return false;
                }

                Object this$server_id = this.getServer_id();
                Object other$server_id = other.getServer_id();
                if (this$server_id == null) {
                    if (other$server_id != null) {
                        return false;
                    }
                } else if (!this$server_id.equals(other$server_id)) {
                    return false;
                }

                Object this$pos = this.getPos();
                Object other$pos = other.getPos();
                if (this$pos == null) {
                    if (other$pos != null) {
                        return false;
                    }
                } else if (!this$pos.equals(other$pos)) {
                    return false;
                }

                label170:
                {
                    Object this$row = this.getRow();
                    Object other$row = other.getRow();
                    if (this$row == null) {
                        if (other$row == null) {
                            break label170;
                        }
                    } else if (this$row.equals(other$row)) {
                        break label170;
                    }

                    return false;
                }

                label163:
                {
                    Object this$version = this.getVersion();
                    Object other$version = other.getVersion();
                    if (this$version == null) {
                        if (other$version == null) {
                            break label163;
                        }
                    } else if (this$version.equals(other$version)) {
                        break label163;
                    }

                    return false;
                }

                Object this$connector = this.getConnector();
                Object other$connector = other.getConnector();
                if (this$connector == null) {
                    if (other$connector != null) {
                        return false;
                    }
                } else if (!this$connector.equals(other$connector)) {
                    return false;
                }

                Object this$name = this.getName();
                Object other$name = other.getName();
                if (this$name == null) {
                    if (other$name != null) {
                        return false;
                    }
                } else if (!this$name.equals(other$name)) {
                    return false;
                }

                label142:
                {
                    Object this$snapshot = this.getSnapshot();
                    Object other$snapshot = other.getSnapshot();
                    if (this$snapshot == null) {
                        if (other$snapshot == null) {
                            break label142;
                        }
                    } else if (this$snapshot.equals(other$snapshot)) {
                        break label142;
                    }

                    return false;
                }

                label135:
                {
                    Object this$db = this.getDb();
                    Object other$db = other.getDb();
                    if (this$db == null) {
                        if (other$db == null) {
                            break label135;
                        }
                    } else if (this$db.equals(other$db)) {
                        break label135;
                    }

                    return false;
                }

                Object this$sequence = this.getSequence();
                Object other$sequence = other.getSequence();
                if (this$sequence == null) {
                    if (other$sequence != null) {
                        return false;
                    }
                } else if (!this$sequence.equals(other$sequence)) {
                    return false;
                }

                label121:
                {
                    Object this$table = this.getTable();
                    Object other$table = other.getTable();
                    if (this$table == null) {
                        if (other$table == null) {
                            break label121;
                        }
                    } else if (this$table.equals(other$table)) {
                        break label121;
                    }

                    return false;
                }

                Object this$gtid = this.getGtid();
                Object other$gtid = other.getGtid();
                if (this$gtid == null) {
                    if (other$gtid != null) {
                        return false;
                    }
                } else if (!this$gtid.equals(other$gtid)) {
                    return false;
                }

                label107:
                {
                    Object this$file = this.getFile();
                    Object other$file = other.getFile();
                    if (this$file == null) {
                        if (other$file == null) {
                            break label107;
                        }
                    } else if (this$file.equals(other$file)) {
                        break label107;
                    }

                    return false;
                }

                Object this$thread = this.getThread();
                Object other$thread = other.getThread();
                if (this$thread == null) {
                    if (other$thread != null) {
                        return false;
                    }
                } else if (!this$thread.equals(other$thread)) {
                    return false;
                }

                Object this$query = this.getQuery();
                Object other$query = other.getQuery();
                if (this$query == null) {
                    if (other$query != null) {
                        return false;
                    }
                } else if (!this$query.equals(other$query)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(Object other) {
        return other instanceof SourceDto;
    }

    @Override
    public int hashCode() {
        return Objects.hash(version, connector, name, ts_ms, snapshot, db, sequence, table, server_id, gtid, file, pos, row, thread, query);
    }

    @Override
    public String toString() {
        return "SourceDto(version=" + this.getVersion() + ", connector=" + this.getConnector() + ", name=" + this.getName() + ", ts_ms=" + this.getTs_ms() + ", snapshot=" + this.getSnapshot() + ", db=" + this.getDb() + ", sequence=" + this.getSequence() + ", table=" + this.getTable() + ", server_id=" + this.getServer_id() + ", gtid=" + this.getGtid() + ", file=" + this.getFile() + ", pos=" + this.getPos() + ", row=" + this.getRow() + ", thread=" + this.getThread() + ", query=" + this.getQuery() + ")";
    }

    public SourceDto(String version, String connector, String name, Long ts_ms, String snapshot, String db, Object sequence, String table, Long server_id, String gtid, String file, Long pos, Long row, Object thread, Object query) {
        this.version = version;
        this.connector = connector;
        this.name = name;
        this.ts_ms = ts_ms;
        this.snapshot = snapshot;
        this.db = db;
        this.sequence = sequence;
        this.table = table;
        this.server_id = server_id;
        this.gtid = gtid;
        this.file = file;
        this.pos = pos;
        this.row = row;
        this.thread = thread;
        this.query = query;
    }

    public SourceDto() {
    }

    public static class SourceDtoBuilder {
        private String version;
        private String connector;
        private String name;
        private Long ts_ms;
        private String snapshot;
        private String db;
        private Object sequence;
        private String table;
        private Long server_id;
        private String gtid;
        private String file;
        private Long pos;
        private Long row;
        private Object thread;
        private Object query;

        SourceDtoBuilder() {
        }

        public SourceDto.SourceDtoBuilder version(String version) {
            this.version = version;
            return this;
        }

        public SourceDto.SourceDtoBuilder connector(String connector) {
            this.connector = connector;
            return this;
        }

        public SourceDto.SourceDtoBuilder name(String name) {
            this.name = name;
            return this;
        }

        public SourceDto.SourceDtoBuilder ts_ms(Long ts_ms) {
            this.ts_ms = ts_ms;
            return this;
        }

        public SourceDto.SourceDtoBuilder snapshot(String snapshot) {
            this.snapshot = snapshot;
            return this;
        }

        public SourceDto.SourceDtoBuilder db(String db) {
            this.db = db;
            return this;
        }

        public SourceDto.SourceDtoBuilder sequence(Object sequence) {
            this.sequence = sequence;
            return this;
        }

        public SourceDto.SourceDtoBuilder table(String table) {
            this.table = table;
            return this;
        }

        public SourceDto.SourceDtoBuilder server_id(Long server_id) {
            this.server_id = server_id;
            return this;
        }

        public SourceDto.SourceDtoBuilder gtid(String gtid) {
            this.gtid = gtid;
            return this;
        }

        public SourceDto.SourceDtoBuilder file(String file) {
            this.file = file;
            return this;
        }

        public SourceDto.SourceDtoBuilder pos(Long pos) {
            this.pos = pos;
            return this;
        }

        public SourceDto.SourceDtoBuilder row(Long row) {
            this.row = row;
            return this;
        }

        public SourceDto.SourceDtoBuilder thread(Object thread) {
            this.thread = thread;
            return this;
        }

        public SourceDto.SourceDtoBuilder query(Object query) {
            this.query = query;
            return this;
        }

        public SourceDto build() {
            return new SourceDto(this.version, this.connector, this.name, this.ts_ms, this.snapshot, this.db, this.sequence, this.table, this.server_id, this.gtid, this.file, this.pos, this.row, this.thread, this.query);
        }

        public String toString() {
            return "SourceDto.SourceDtoBuilder(version=" + this.version + ", connector=" + this.connector + ", name=" + this.name + ", ts_ms=" + this.ts_ms + ", snapshot=" + this.snapshot + ", db=" + this.db + ", sequence=" + this.sequence + ", table=" + this.table + ", server_id=" + this.server_id + ", gtid=" + this.gtid + ", file=" + this.file + ", pos=" + this.pos + ", row=" + this.row + ", thread=" + this.thread + ", query=" + this.query + ")";
        }
    }
}